package com.huatai.datadevelop.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.datadevelop.utils.DateUtils;
import com.huatai.datadevelop.utils.RedisCache;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author hecai
 * @description: TODO
 * @date 2022/11/22 16:52
 * @Version 1.0
 */
@Service("commonService")
public class CommonService {

    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 获取系统可以支撑的contentType
     * @return
     */
    public List<String> getContentTypeList(){
        String value = sysConfigService.getValue("contentType");
        List<String> stringList = Arrays.asList(value.split(","));
        return stringList;
    }

    /**
     * 获取系统可以支撑的请求方式
     * @return
     */
    public List<String> getRequestMethodList(){
        String requestMethod = sysConfigService.getValue("requestMethod");
        List<String> stringList = Arrays.asList(requestMethod.split(","));
        return stringList;
    }


    /**
     * 通过抛出异常判断是否是数字
     * @param str
     * @return
     */
    public boolean isNumber(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 对hashmap的key升序排序
     * @param hashMap
     * @return
     */
    public Set sortASC(HashMap<String,Object> hashMap){
        Set set = hashMap.keySet();
        Object[] objects = set.toArray();
        Arrays.sort(objects);
        return set;
    }

    /**
     * 获取系统名称
     * @return
     */
    public String getTitleValue(String title) {
        String value = sysConfigService.getValue(title);
        return value;
    }

    /**
     * 地铁列车出入情况
     * @return
     */
    public JSONObject getTrainAccess(JSONObject param){
        JSONObject data = new JSONObject();
        String station = param.getString("EQ_station");
        if (StringUtils.isEmpty(station)){
            throw new ServiceException("请传入参数EQ_station");
        }
        Object value = redisCache.get(station);
        if(value!=null){
            data = JSON.parseObject(value.toString());
        }else {
            //随机生成1-7之间的随机数
            Random random = new Random();
            //窦关站进站时间
            int numberDG = random.nextInt(7)+1;
            data.put("thisTrainDG",numberDG+"分");
            data.put("nextTrainDG",numberDG+10+"分");
            //小孟工业园站进站时间
            int numberXM = random.nextInt(6)+1;
            data.put("thisTrainXM",numberXM+"分");
            data.put("nextTrainXM",numberXM+10+"分");

            //窦关站进站时间
            data.put("thisTrainTimeDG",this.getTimeToDateStation(numberDG));
            data.put("nextTrainTimeDG",this.getTimeToDateStation(numberDG+10));
            //小孟工业园站进站时间
            data.put("thisTrainTimeXM",this.getTimeToDateStation(numberXM));
            data.put("nextTrainTimeXM",this.getTimeToDateStation(numberXM+10));
            redisCache.set(station,JSON.toJSONString(data),60);
        }
        return data;
    }

    /**
     * 根据站点时间处理参数
     * @param number
     * @return
     */
    private String getTimeToDateStation(int number){
        LocalDateTime localDateTime = DateUtils.toLocalDateTime(DateUtils.addDateMinutes(new Date(), number));
        int hour = localDateTime.getHour();
        int minute = localDateTime.getMinute();
        String dateTime = hour+":"+minute;
        return dateTime;
    }
}
